package com.acheli.rideable.functionLathes.component.stabilizer;


import com.acheli.registry.keybind.KeyBinding;
import com.acheli.rideable.entites.ComponentEntity;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.functionLathes.ComponentFL;
import com.acheli.utils.math.angle.AbstractAngle;
import com.acheli.utils.math.angle.Angle;
import com.acheli.utils.physics.PhysicsEngine;
import com.acheli.utils.rotate.RotateWith;
import com.acheli.utils.rotate.TorqueCalculator;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.phys.Vec3;
import org.joml.Quaternionf;
import org.joml.Vector3f;

public class StabilizerBase extends ComponentFL { //气动稳定翼
    AbstractAngle xAxisAngle = new Angle(0, new Angle(45), new Angle(-45));
    AbstractAngle yAxisAngle = new Angle(0, new Angle(45), new Angle(-45));
    AbstractAngle zAxisAngle = new Angle(0, new Angle(45), new Angle(-45));

    public StabilizerBase(String ID, RideableEntity vehicle, Vec3 relativePosition, double relativeAABBXLength, double relativeAABBYLength, double relativeAABBZLength) {
        super(ID, vehicle, relativePosition);
        enableVelocity = true;
        setRelativeAABB(relativeAABBXLength, relativeAABBYLength, relativeAABBZLength);
    }

    public void setUpAngles(AbstractAngle xAxisAngle, AbstractAngle yAxisAngle, AbstractAngle zAxisAngle) {
        this.xAxisAngle = xAxisAngle;
        this.yAxisAngle = yAxisAngle;
        this.zAxisAngle = zAxisAngle;
    }

    @Override
    public void onClientTick() {
        super.onClientTick();
        if (KeyBinding.CYCLIC_PUSH.isDown()) {
            zAxisAngle.add(new Angle(-5));

        }
        if (KeyBinding.CYCLIC_PULL.isDown()) {
            zAxisAngle.add(new Angle(5));

        }
        if (KeyBinding.CYCLIC_LEFT.isDown()) {


        }
        if (KeyBinding.CYCLIC_RIGHT.isDown()) {


        }
    }

    @Override
    public void renderTick(ComponentEntity dynamicBody, PoseStack matrices, float tickDelta) {
        super.renderTick(dynamicBody, matrices, tickDelta); //获取到速度
        if (dynamicBody.level().isClientSide) {
            if (dynamicBody.getVehicle() instanceof RideableEntity rideableEntity) {
                rideableEntity.doFactory(factoryNotNull -> {
                    Quaternionf rotate = factoryNotNull.getProductFunction().getFL().totalRotation;
                            rotate.rotateYXZ(
                                    (float) yAxisAngle.getArcAngleNum(),
                                    (float) xAxisAngle.getArcAngleNum(),
                                    (float) zAxisAngle.getArcAngleNum()
                            );
                    Vec3 direction = getVelocity().multiply(PhysicsEngine.every(-1)).normalize();
//                setRelativePosition(RideableEntity.testPos);
                    Vector3f angles = TorqueCalculator.calculateLocalRotationDirection(
                            getRelativePosition(),
                            rideableEntity.position(),
                            TorqueCalculator.fromVector3f(
                                    TorqueCalculator.CoordinateTransform.worldToLocal(
                                            direction.toVector3f(), rotate
                                    )
                            ), // 力的大小
                            new Quaternionf()
                    );

                    double horizontalFaceArea = getRelativeAABB_Y_Length() * getRelativeAABB_Z_Length();
                    double verticalFaceArea = getRelativeAABB_X_Length() * getRelativeAABB_Z_Length();
//                   double headFaceArea = getRelativeAABB_X_Length() * getRelativeAABB_Y_Length();

                    double rv = rideableEntity.getDeltaMovement().length() * 3;
                    factoryNotNull.getProductFunction().getFL().addMomentum(
                            new Vec3(
                                    0,
                                    angles.y/880000 * rv * horizontalFaceArea,
                                    angles.x/4800000 * rv * verticalFaceArea
                            )
                    );
                });



            }
        }
    }

}
